package com.aaa.controller;

import com.aaa.entity.ReturnData;
import com.aaa.service.UserInfoService;
import com.aaa.service.impl.UserInfoServiceImpl;
import com.aaa.util.MyConstants;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/7/16 10:44
 * @description：用户业务控制器
 * @modified By：
 * @version: 1.0
 */
@WebServlet(urlPatterns = "/myUser")
public class UserController  extends BaseController {
    UserInfoService userInfoService= new UserInfoServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> map = initParam(req, resp);
        //判断方法名
        String method=map.get("method")+"";
        ReturnData returnData = new ReturnData();
        switch (method){
            case "findAllUser":
                List<Map<String, Object>> allUser = userInfoService.findAllUserPage(map);
                //回传json数据
                returnData.setCode(0);
                //获取数据库表的总条数
                int count = userInfoService.getCount(map);
                returnData.setCount(count);
                returnData.setData(allUser);
              /*  //将回传的数据转换成json字符串
                String jsonString = JSON.toJSONStringWithDateFormat(returnData,"yyyy-MM-dd HH:mm:ss");
                //通过HttpServletResponse将json字符串响应给前端浏览器
                resp.getWriter().print(jsonString);*/
                super.toJsonString(returnData,resp);
                break;
            case "addUser":
                //保存数据
                boolean addUser = userInfoService.addUser(map);
                if(addUser){
                    super.returnSuccess(returnData);

                }else
                {
                  super.returnFail(returnData);
                }
                super.toJsonString(returnData,resp);
                break;
            case "updateUser":
                //保存数据
                boolean updateUser = userInfoService.updateUser(map);
                if(updateUser){
                    super.returnSuccess(returnData);

                }else
                {
                  super.returnFail(returnData);
                }
                super.toJsonString(returnData,resp);
                break;
            case "deleteUser":
                int  id = Integer.parseInt(map.get("id"));
                boolean deleteUserById = userInfoService.deleteUserById(id);
                if(deleteUserById){
                    super.returnSuccess(returnData);
                }else
                {
                    super.returnFail(returnData);
                }
                super.toJsonString(returnData,resp);
                break;
            case "deleteUserBatch":
                String ids=map.get("id")+"";
                boolean deleteUserBatch = userInfoService.deleteUserBatch(ids);
                if(deleteUserBatch){
                    super.returnSuccess(returnData);
                }else
                {
                    super.returnFail(returnData);
                }
                super.toJsonString(returnData,resp);
                break;
            default:
                System.out.println("参数错误");
                break;
        }
    }
}
